import { resolve } from 'path'
import { type ConfigEnv, defineConfig, type UserConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import tailwindcss from '@tailwindcss/vite'
import { tanstackRouter } from '@tanstack/router-plugin/vite'
import { fileURLToPath, URL } from 'node:url'
import AutoImport from 'unplugin-auto-import/vite'
import { viteVConsole } from 'vite-plugin-vconsole'

// https://vite.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  // const env = loadEnv(mode, __dirname as string)
  return {
    base: '/',
    plugins: [
      tanstackRouter({
        target: 'react',
        autoCodeSplitting: true,
      }),
      react(),
      tailwindcss(),
      viteVConsole({
        entry: [resolve('src/main.tsx')],
        localEnabled: false, // 是否在本地开发环境启用
        // enabled: false, // build prod
        enabled: mode === 'test', // build prod
        config: {
          theme: 'light',
        },
      }),
      AutoImport({
        dts: true,
        imports: ['react', 'react-router'],
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.md$/, // .md
        ],
        resolvers: [],
        eslintrc: {
          enabled: true,
          filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
          globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
        },
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@public': fileURLToPath(new URL('./public', import.meta.url)),
        '~': fileURLToPath(new URL('./node_modules', import.meta.url)),
      },
    },
    build: {
      minify: 'esbuild',
      rollupOptions: {
        output: {
          manualChunks: {
            // React 核心库
            'vendor-react': ['react', 'react-dom', 'react/jsx-runtime'],
            // 路由
            'vendor-router': [
              '@tanstack/react-router',
              '@tanstack/react-query',
            ],
            // UI 组件库
            'vendor-ui': ['lucide-react', 'sonner'],
            // 状态管理
            'vendor-state': ['zustand', 'usehooks-ts'],
            // 表单处理
            'vendor-form': ['react-hook-form', '@hookform/resolvers', 'zod'],
            // 动画库
            'vendor-animation': ['framer-motion', 'tw-animate-css'],
            // 工具库
            'vendor-utils': [
              'class-variance-authority',
              'clsx',
              'tailwind-merge',
            ],
            // 网络请求
            'vendor-http': ['axios'],
            // 其他第三方库
            'vendor-other': ['react-top-loading-bar', 'next-themes'],
          },
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[extname]/[name]-[hash][extname]',
        },
      },
    },
    server: {
      open: false, // 是否自动在浏览器打开
      port: 5183, // 端口号
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: '',
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, '/api'),
        },
      },
    },
  }
})
